<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
if (!ispost())
{
    exit();
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chk = new CheckMode_3d($curruser['acl'], 3, $gametype);
$chk->checkShow3dPage();
$show3D3S = $chk->show3D3S;
$g_betname_3or5 = judge_type($gametype);
unset($chk);
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
require_once( "class_3d/RateConfig_3d.php" );
require_once( "class_3d/SRate_3d.php" );
$ui = new UI_3D(true);
$conf = new Config();
$RateConfig_3d = new RateConfig_3d($gametype);
$SRate_3d = new SRate_3d($gametype);
$today = date("Y-m-d");
$bettypegroup = getvar("palygroup");
$betmin = $conf->get("betmin_{$tbl}");
$betmin2face = $conf->get("betmin2face_{$tbl}");
if ($betmin2face <= 0)
{
    $betmin2face = $betmin;
}
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("帐号已被暂停使用");
    exit();
}
else if (1 < $curruser['acl'])
{
    $ui->showErrorPage("非会员无法下注");
    exit();
}
$disk = intval(getvar("disk"));
if (!isset($curruser["disk{$disk}"]) || $curruser["disk{$disk}"] != 1)
{
    $ui->showErrorPage("盘类错误");
    exit();
}
$diskStr = $RateConfig_3d->diskArr[$disk];
$diskStr2 = $g_disk_name[$disk];
if ($show3D3S == 1)
{
    unset($RateConfig_3d->playtypeArr['r1pass']);
    unset($RateConfig_3d->playGroupArr['r1pass']);
    unset($RateConfig_3d->playtypeArr['r3p']);
    unset($RateConfig_3d->playGroupArr['r3p']);
}
unset($RateConfig_3d->playGroupArr['g3']);
unset($RateConfig_3d->playGroupArr['g6']);
unset($RateConfig_3d->playGroupArr['rcmp']);
if ($bettypegroup == "" || !isset($RateConfig_3d->playGroupArr[$bettypegroup]))
{
    $ui->showErrorPage("{$tblName}-不正确的玩法页面");
    exit();
}
include_once( "BetChk.php" );
$BetChk = new BetChk($today, $gametype);
if ($lotto_3d = $BetChk->chkBetDate($today))
{
    $lotto_id = $lotto_3d['id'];
    $round = $lotto_3d['round'];
    $showBetTable = array();
    $rateArr = array();
    $amountsDataArr = array();
    $getRatesNumArr = array();
    $showAreaType = 0;
    if ($bettypegroup == "r2a")
    {
        $showAreaType = intval(getvar("showAreaType"));
    }
    $showAllSubArr = $RateConfig_3d->showAllSubArr;
    if (in_array($bettypegroup, $showAllSubArr))
    {
        $showSubid = intval(getvar("subid"));
        $showSubid = isset($RateConfig_3d->subArr[$showSubid]) ? $showSubid : "0";
    }
    else
    {
        $showSubid = 0;
    }
    foreach ($_POST as $key => $val)
    {
        $betOKFlag = true;
        $tempArr = array();
        $val = intval($val);
        if (empty($val) || $val < 1)
        {
            $betOKFlag = false;
            continue;
        }
        $dataArrTemp = explode("_", substr($key, 1));
        if (count($dataArrTemp) != 2)
        {
            $betOKFlag = false;
            continue;
        }
        $dataBetSubArr = explode("V", $dataArrTemp[0]);
        if (1 < count($dataBetSubArr))
        {
            $dataArr = array(0 => $dataBetSubArr[0], 1 => $dataArrTemp[1], 2 => $dataBetSubArr[1]);
        }
        else
        {
            $dataArr = array(0 => $dataBetSubArr[0], 1 => $dataArrTemp[1], 2 => $showSubid);
        }
        if (!isset($RateConfig_3d->subArr[$dataArr[2]]))
        {
            $betOKFlag = false;
            $timeTemp = rand(10, 99) . "1" . rand(0, 9);
            $errMsg = "({$tbl})[betbeforedo]No.{$timeTemp} 会员下注:不正确的subid:id-{$dataArr[0]}, subid-{$dataArr[2]}";
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
            exit();
        }
        $dataArr[0] = $dataArr[0] + $showAreaType;
        $rateArrIdx = $dataArr[0] . "_" . $dataArr[2];

        if (!isset($g_betname_3or5[$rateArrIdx]))
        {
            $betOKFlag = false;
            $timeTemp = rand(10, 99) . "2" . rand(0, 9);
            $errMsg = "({$tbl})[betbeforedo]No.{$timeTemp} 会员下注:不被许\可的玩法:id-{$dataArr[0]}, subid-{$dataArr[2]}";
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
            exit();
        }

        if (!in_array($dataArr[0], $RateConfig_3d->playGroupArr[$bettypegroup]))
        {
            $betOKFlag = false;
            $timeTemp = rand(10, 99) . "3" . rand(0, 9);
            $errMsg = "({$tbl})[betbeforedo]No.{$timeTemp} 会员下注:不正确的玩法:id-" . $dataArr[0];
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
            exit();
        }
        if ($dataArr[0] == 46)
        {
            $temVal = explode(",", $_POST[$key]);
            if (count($temVal) != 4)
            {
                write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "({$tbl})一字过关，下注内容格式错误" . count($temVal));
                exit();
            }
            $val = intval($temVal[0]);
            $temVal[1] = intval($temVal[1]);
            $temVal[2] = intval($temVal[2]);
            $temVal[3] = intval($temVal[3]);
            $numArr = array($temVal[1], $temVal[2], $temVal[3]);
            if (empty($val) || $val < 1)
            {
                $betOKFlag = false;
                continue;
            }
            if (!$RateConfig_3d->chkMixIdx($dataArr[0], $numArr))
            {
                $betOKFlag = false;
                $timeTemp = rand(10, 99) . "4" . rand(0, 9);
                $errMsg = "({$tbl})[betbeforedo]No.{$timeTemp} 会员下注:不正确的内容(一字过关):id-{$dataArr[0]},num-" . implode(",", $numArr);
                write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
                $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
                exit();
            }
            $dataArr[1] = $numArr;
        }
        else if (!$RateConfig_3d->checkBetNumber($dataArr[0], $dataArr[1]))
        {
            $betOKFlag = false;
            $timeTemp = rand(10, 99) . "5" . rand(0, 9);
            $errMsg = "({$tbl})[betbeforedo]No.{$timeTemp} 会员下注:不正确的内容:id-" . $dataArr[0] . ",num-" . $dataArr[1];
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
            exit();
        }
        $betmin_tmp = in_array($dataArr[0], $g_2face_3d) ? $betmin2face : $betmin;
        if ($val < $betmin_tmp)
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $g_betname_3or5[$rateArrIdx], "nums" => $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]), "rate" => "--", "betmoney" => "<font class=\"red\">下注金额不能小於最低限额({$betmin_tmp}元)</font>", "betflag" => false);
            continue;
        }
        if (!isset($rateArr[$rateArrIdx]))
        {
            $tempRateBaseArr = $SRate_3d->getRates($lotto_id, $disk, $dataArr[0], $dataArr[2]);
            if ($tempRateBaseArr == false)
            {
                $betOKFlag = false;
                $showBetTable[] = array("bettype" => $g_betname_3or5[$rateArrIdx], "nums" => $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]), "rate" => "--", "betmoney" => "<font class=\"red\">无赔率资料无法下注</font>", "betflag" => false);
                $errMsg = "({$tbl})[betbeforedo]会员下注:无该玩法之赔率值:id-{$dataArr[0]}, subid-{$dataArr[2]}";
                write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
                continue;
            }
            $user_id = $SRate_3d->getAcl5ID($curruser['id'], $curruser['acl']);
            if ($tempRateBaseArr2 = $SRate_3d->getRatesS($lotto_id, $dataArr[0], $user_id, $dataArr[2]))
            {
                $tempRateArr = array();
                foreach ($tempRateBaseArr as $key2 => $val2)
                {
                    $tempRateArr[$key2] = $tempRateBaseArr[$key2] - $tempRateBaseArr2[$key2];
                }
                $rateArr[$rateArrIdx] = $tempRateArr;
            }
            else
            {
                $rateArr[$rateArrIdx] = $tempRateBaseArr;
            }
            $getRatesNumArr[$rateArrIdx] = $SRate_3d->getRatesNumArr($lotto_id, $disk, $dataArr[0], $dataArr[2]);
            $getRatesSNumArr[$rateArrIdx] = $SRate_3d->getRatesSNumArr($lotto_id, $user_id, $dataArr[0], $dataArr[2]);
        }
        $rate = $RateConfig_3d->getRateFromRateArr($dataArr[0], $dataArr[1], $rateArr[$rateArrIdx], $getRatesNumArr[$rateArrIdx], $getRatesSNumArr[$rateArrIdx]);
        if ($rate <= 1)
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $g_betname_3or5[$rateArrIdx], "nums" => $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]), "rate" => "--", "betmoney" => "<font class=\"red\">赔率值错误无法下注</font>", "betflag" => false);
            $errMsg = "({$tbl})[betbeforedo]会员下注:赔率值错误:id-{$dataArr[0]},num-{$dataArr[1]}, subid-{$dataArr[2]}";
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            continue;
        }
        if (!$betOKFlag)
        {
            continue;
        }
        else if ($dataArr[0] != 46)
        {
            $showBetTable[] = array("bettype" => $g_betname_3or5[$rateArrIdx] . "<input type=\"hidden\" name=\"r{$dataArr[0]}_{$dataArr[1]}_{$dataArr[2]}\" value=\"{$val}\" />", "nums" => $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]), "rate" => $rate . "<input type=\"hidden\" name=\"rate{$dataArr[0]}v{$dataArr[1]}v{$dataArr[2]}\" value=\"{$rate}\" />", "betmoney" => $val, "betflag" => true);
        }
        else
        {
            $tempStrData = "";
            foreach ($dataArr[1] as $tempVal)
            {
                $tempStrData .= str_pad($tempVal, 2, "0", STR_PAD_LEFT);
            }
            $showBetTable[] = array("bettype" => $g_betname_3or5[$rateArrIdx] . "<input type=\"hidden\" name=\"r{$dataArr[0]}_{$tempStrData}_{$dataArr[2]}\" value=\"{$val}\" />", "nums" => $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]), "rate" => $rate . "<input type=\"hidden\" name=\"rate{$dataArr[0]}v{$tempStrData}v{$dataArr[2]}\" value=\"{$rate}\" />", "betmoney" => $val, "betflag" => true);
        }
    }
    if ($bettypegroup == "r3" && intval(getvar("qFast")))
    {
        $qFast = "1";
    }
    else
    {
        $qFast = "";
    }
    if ($qFast)
    {
        $qFastStr = "&qFast={$qFast}";
    }
    else
    {
        $qFastStr = "";
    }
    $ui->header();
    $ui->showNews3D($curruser['acl'],1);
    echo "<div></div>";
    echo "<form action=\"op.php?op=member_3d&fp=bet_do&palygroup=" . $bettypegroup . "&gametype=" . $gametype . "&disk=" . $disk . "\" method=\"post\" onsubmit=\"this.submit.disabled=true;this.reset.disabled=true\">";
    echo "<input type=\"hidden\" value=\"" . $bettypegroup . "\" name=\"bettypegroup\" />";
    echo "<input type=\"hidden\" value=\"" . $gametype . "\" name=\"gametype\" />";
    echo "<input type=\"hidden\" value=\"" . $disk . "\" name=\"disk\" />";
    echo "<input type=\"hidden\" value=\"" . $showAreaType . "\" name=\"showAreaType\" />";
    echo "<input type=\"hidden\" value=\"" . $qFast . "\" name=\"qFast\" />";
    if ($bettypegroup == "r1p")
    {
        $showType = intval(getvar("showType"));
        echo "<input type=\"hidden\" value=\"" . $showType . "\" name=\"showType\" />";
    }
    echo "<input type=\"hidden\" value=\"" . $showSubid . "\" name=\"subid\" />";
    echo "<table class=\"tblMy3D\">";
    echo "<thead><tr class=\"title0 bgImg_" . $gametype . "\"><th colspan=5>" . $tblName . " 第" . $round . "期 您下注的是：</th></tr>";
    echo "<tr class=\"bgClc_" . $gametype . "\"><th>下注方式</th><th>下注内容</th><th>盘类</th><th>赔率</th><th>下注金额</th></tr></thead>";
    echo "<tbody>";
    $totalmon = 0;
    foreach ($showBetTable as $val)
    {
        echo "<tr class=\"td2\">";
        echo "<td>" . $val['bettype'] . "</td>";
        echo "<td>" . $val['nums'] . "</td>";
        echo "<td>" . $diskStr2 . "</td>";
        echo "<td>" . $val['rate'] . "</td>";
        echo "<td align=\"right\">" . $val['betmoney'] . "</td>";
        if ($val['betflag'])
        {
            $totalmon += $val['betmoney'];
        }
        echo "</tr>";
    }
    $timebtnType = getvar("timebtnType") == "checkbox" ? "checkbox" : "text";
    $backurl = "op.php?op=member_3d&fp=bet&palygroup={$bettypegroup}&timebtnType={$timebtnType}&gametype={$gametype}&disk={$disk}&showAreaType={$showAreaType}&subid={$showSubid}{$qFastStr}";
    if (0 < $totalmon)
    {
        echo "<tr class=\"bgClc_" . $gametype . "\" align=\"right\"><td colspan=\"4\">合计</td><td>" . $totalmon . "</td></tr>";
        echo "<tr class=\"bgClc_24\"><td colspan=\"6\" align=\"center\" style=\"color:red;\"><input type=\"checkbox\" name=\"recentNew\" value=\"1\" checked />强制(赔率异动时，依新赔率下注)</td></tr>";
		$btnStr = "<input type=\"submit\" name=\"submit\" value=\"确定下注\" />&nbsp;&nbsp;<input name=\"reset\" type=\"reset\" value=\"取消下注\" onclick=\"javascript:window.top.mainFrame.location.href='" . $backurl . "';\" />";
        $focusStr = "document.forms[0].submit.focus();";
    }
    else
    {
        echo "<tr class=\"red\"><td colspan=\"5\" align=\"center\">无任何可下注资料</td></tr>";
        $btnStr = "<input name=\"reset\" type=\"reset\" value=\"回上一页\" onclick=\"javascript:window.top.mainFrame.location.href='" . $backurl . "';\" />";
        $focusStr = "";
    }
    echo "</tbody>";
    echo "</table>";
    echo $btnStr;
    echo "<input type=\"hidden\" value=\"" . $timebtnType . "\" name=\"timebtnType\" id=\"timebtnType\" />";
    echo "</form>";
    echo "<script>";
    echo $focusStr;
    echo "</script>";
    $ui->footer();
}
?>